package engine.newUi
{
	import engine.data.ModelLocator;
	import engine.net.datatype.WString;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.ui.AlertUI;
	import engine.ui.UIManage;
	
	import flash.events.MouseEvent;
	
	import newx.com.config.StaticIni;
	import newx.com.control.table.NxTableEvent;
	import newx.com.control.window.NxWindowEvent;

	public class FamilySwitchHeadLogic
	{
		private var _ui:FamilySwitchHeadUI;
		public static var _self:FamilySwitchHeadLogic;
		/**数据模型*/
		private 	var _model			:engine.data.ModelLocator;
		/***当前选中的副帮主的名字**/
		private var _familyName:String ="";
		public function FamilySwitchHeadLogic()
		{
		}
		
		public static function getInstance():FamilySwitchHeadLogic
		{
			if(_self == null)
			{
				_self =new FamilySwitchHeadLogic();
			}
			return _self;
		}
		
		public function uiinit():void
		{
			_model = ModelLocator.getInstance();

		}
		
		public function showUI(arr:Array):void
		{
			if(arr.length<1)
			{
				ui.tab_switch.visible = false;
				ui.lab_nothing.visible = true;
				ui.btn_ok.enabled = false;
			}else
			{
				ui.tab_switch.dataProvider = arr;	
				ui.tab_switch.visible = true;
				ui.lab_nothing.visible = false;
				ui.btn_ok.enabled = true;
			}
			
			ui.show();
		}
		
		public function BeforeShow():void
		{
			ui.btn_ok.enabled = false;
			addListenter();
		}
		
		public function AfterHide():void
		{
			removeListenter();
		}
		
		private function onOkClicHandle(e:MouseEvent):void
		{
			if(_familyName == "")return;
			UIManage.getInstance().setCustUIMask(this.ui.parent);
			var	titleT		:String			=	StaticIni.getSwfLangStr("MessageUI_Notice");
			var	iniDescT	:String			=	StaticIni.getSwfLangStr("UIStr_1000564");
			AlertUI.showAlert(titleT,iniDescT,AlertUI.YES|AlertUI.NO,function(type:int, str:String, clickData:*):void{
				if(type == AlertUI.YES)
				{
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GUILD_CHANGECOMMANDER,
						new WString(_familyName));
					ui.hide();
				}
				UIManage.getInstance().cancelCustUIMask();
			});
		}
		
		/**
		 *表行记录点击 
		 * @param e
		 * 
		 */		
		private function onTbClick(evt:NxTableEvent):void
		{
		
			if(!evt.lineArrData)
			{
				//				MenuList.hide();
				_familyName = "";
				ui.btn_ok.enabled = false;
				return;
			}
			var data : Object = evt.lineArrData[1]["data"];
			
			if(!data)
			{
				_familyName = "";
				ui.btn_ok.enabled = false;
				return;
			}
			_familyName = String(data);
			ui.btn_ok.enabled = true;
		}
		
		private function onClickCloseHandle(e:NxWindowEvent):void
		{
			ui.hide();
		}
		
		private function onCloseClickHandle(e:MouseEvent ):void
		{
			ui.hide();
		}
		
		/**
		 *添加监听 
		 * 
		 */
		private function addListenter():void
		{
			ui.tab_switch			.addEventListener(NxTableEvent.TABLE_CLICK,	onTbClick);		
			ui.btn_ok.addEventListener(MouseEvent.CLICK,onOkClicHandle);
			ui.btn_close.addEventListener( MouseEvent.CLICK,	onCloseClickHandle);
			ui.windowBG	.addEventListener(	NxWindowEvent.WINDOW_CLOSE,	onClickCloseHandle);
		}
		
		/**
		 *移除监听 
		 * 
		 */
		private  function removeListenter():void
		{
			ui.tab_switch			.removeEventListener(NxTableEvent.TABLE_CLICK,	onTbClick);
			ui.btn_ok.removeEventListener(MouseEvent.CLICK,onOkClicHandle);
			ui.btn_close.removeEventListener( MouseEvent.CLICK,	onCloseClickHandle);
			ui.windowBG	.removeEventListener(	NxWindowEvent.WINDOW_CLOSE,	onClickCloseHandle);
		}
		public function get ui():FamilySwitchHeadUI
		{
			if(_ui==null)
			{
				_ui	=	new FamilySwitchHeadUI();
			}
			return _ui
		}
	}
}